//品牌控制层
app.controller('brandController', function ($scope,brandService) {

    $controller('baseController',{$scope:$scope});//继承

    //查询品牌列表
    $scope.findAll = function () {
        brandService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        )
    }

    $scope.reloadList = function () {
        //切换页码
        $scope.search($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
    }


    //分页控件配置
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 10,
        itemsPerPage: 10,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
            $scope.reloadList();//重新加载
        }
    };

    //分页
    $scope.findPage = function (page, rows) {
        brandService.findPage(page,rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //保存品牌信息
    $scope.save = function () {

        var object = null;
        if ($scope.entity.id!=null){//如果有ID
            object = brandService.update($scope.entity);

        }else{
            object = brandService.add($scope.entity);
        }

        object.success(
            function (response) {
                if (response.success) {
                    //重新查询
                    $scope.reloadList();//重新加载
                }else {
                    alert(response.message);
                }
            }
        )
    }

    //根据id查询品牌信息
    $scope.findOne=function (id) {
        brandService.findOne(id).success(
            function (response) {
                $scope.entity=response;
            }
        )
    }

    //创建一个存放ID的集合
    $scope.selectIds=[];

    //更新复选
    $scope.updateSelection=function ($event, id) {
        if ($event.target.checked){
            $scope.selectIds.push(id);
        }else {
            //如果取消选中,则找到取消ID在数组中的索引位置
            var idx = $scope.selectIds.indexOf(id);
            //然后删除这个ID
            $scope.selectIds.splice(idx,1);
        }
    }

    //批量删除选中的商品信息
    $scope.dele=function () {
        //获取选中的复选框
        brandService.dele($scope.selectIds).success(
            function (response) {
                if (response.success){
                    //如果删除成功,则刷新列表
                    $scope.reloadList();
                }else {
                    alert(response.message);
                }
            }
        )
    }

    //定义搜索对象
    $scope.searchEntity={};
    //条件查询
    $scope.search=function (page,rows) {
        brandService.search(page,rows,$scope.searchEntity).success(
            function (response) {
                $scope.paginationConf.totalItems=response.total;//总记录数
                $scope.list=response.rows;
            }
        )
    }

});